const api = require("../server/Api.js");

//统一下单 => 接着调起支付宝支付
function pay(orderId, userId) {
  return api.payOrder(orderId, userId).then(res => {
    let bean = JSON.parse(res.result);
    // return wxPay(bean);
    return ttPay(bean)
  });
}
//调起微信支付
function wxPay(params) {
  return new Promise(function (success, fail) {
    wx.requestPayment({
      timeStamp: params.timeStamp,
      nonceStr: params.nonceStr,
      package: params.package,
      signType: params.signType,
      paySign: params.paySign,
      success: function (res) {
        success(res)
      },
      fail: function (res) {
        fail(res)
      }
    })
  })
}
// 调起支付宝支付
function ttPay(params) {
  return new Promise(function (success, fail) {
    tt.requestPayment({
      data: {
        app_id: params.app_id,
        method: params.method,
        sign: params.sign,
        sign_type: params.sign_type,
        timestamp: params.timestamp,
        trade_no: params.trade_no,
        merchant_id: params.merchant_id,
        uid: params.uid,
        total_amount: params.total_amount,
        pay_channel: params.pay_channel,
        pay_type: params.pay_type,
        params: JSON.stringify({
          // 如果是新版支付宝，url 示例：
          url: 'app_id=2018041302549907&biz_content=%7B%22body%22%3A%22novel%22%2C%22subject%22%3A%22%E6%B5%8B%E8%AF%95%E7%9A%84%E5%95%86%E5%93%81%22%2C%22out_trade_no%22%3A%22201808211756233909095950%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%2C%22seller_id%22%3A%22jrtoutiaoyxgs%40bytedance.com%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=utf-8&format=JSON&method=alipay.trade.app.pay&notify_url=https%3A%2F%2Ftp-pay-test.snssdk.com%2Fcallback%2Fali_pay&sign=ZfVkvu%2FSzBqFuqQMgr6MvsXomlr6BCuz7GYDnpsxd3SLVfCssV0q2cnxZyfjh%2FY%2Bk7PO1IeEl4rppQg%2FXgRuIqMXyKdhmigj4oPdQVJEkbSQEcCW4m8mwpXLNjlLH%2FHae3u3hjrMDVPuVXeIxjoq1NLPXy09GY5u1MX8E2lkn8xtmOxA2cXXRIrAa8gTplUoXWkSSkZMgvSTzQ9RjRmlKtK4nERdDWh5RBXLNDU%2FD2FfqIeZuLNZh%2BW8j4dYGtPDm9nWYRz0tLizJDm6E76aTM3qvLi0havCCrHgxZ5d8tVN7GNztA6olbGOiXubEGUq4yBqCojiALEEVpKqfQdZGQ%3D%3D&sign_type=RSA2&timestamp=2018-08-21+17%3A56%3A24&version=1.0'
        }),
      },
      success(res) {
        console.log(res.errMsg);
      },
    })
  })
}

module.exports = {
  pay: pay
}